home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch3 / PalEdit.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-06-21  |  39.3 KB  |  1,196 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmPalEdit 
  4.    Caption         =   "PalEdit"
  5.    ClientHeight    =   5805
  6.    ClientLeft      =   1305
  7.    ClientTop       =   780
  8.    ClientWidth     =   7020
  9.    LinkTopic       =   "Form1"
  10.    PaletteMode     =   1  'UseZOrder
  11.    ScaleHeight     =   388
  12.    ScaleMode       =   0  'User
  13.    ScaleWidth      =   468
  14.    Begin VB.PictureBox picVisible 
  15.       AutoRedraw      =   -1  'True
  16.       Height          =   4515
  17.       Left            =   0
  18.       MousePointer    =   2  'Cross
  19.       Picture         =   "PalEdit.frx":0000
  20.       ScaleHeight     =   297
  21.       ScaleMode       =   3  'Pixel
  22.       ScaleWidth      =   279
  23.       TabIndex        =   1
  24.       Top             =   0
  25.       Width           =   4245
  26.    End
  27.    Begin MSComDlg.CommonDialog dlgOpenFile 
  28.       Left            =   4200
  29.       Top             =   360
  30.       _ExtentX        =   847
  31.       _ExtentY        =   847
  32.       _Version        =   393216
  33.       CancelError     =   -1  'True
  34.       FontSize        =   8.37851e-39
  35.    End
  36.    Begin VB.PictureBox picSwatch 
  37.       AutoRedraw      =   -1  'True
  38.       Height          =   2280
  39.       Left            =   4560
  40.       Picture         =   "PalEdit.frx":0446
  41.       ScaleHeight     =   2220
  42.       ScaleWidth      =   2400
  43.       TabIndex        =   15
  44.       Top             =   2505
  45.       Width           =   2460
  46.    End
  47.    Begin VB.PictureBox picSystemColors 
  48.       AutoRedraw      =   -1  'True
  49.       Height          =   2460
  50.       Left            =   4560
  51.       Picture         =   "PalEdit.frx":088C
  52.       ScaleHeight     =   160
  53.       ScaleMode       =   3  'Pixel
  54.       ScaleWidth      =   160
  55.       TabIndex        =   14
  56.       Top             =   0
  57.       Width           =   2460
  58.    End
  59.    Begin VB.PictureBox picColors 
  60.       BorderStyle     =   0  'None
  61.       Height          =   975
  62.       Left            =   0
  63.       ScaleHeight     =   65
  64.       ScaleMode       =   3  'Pixel
  65.       ScaleWidth      =   468
  66.       TabIndex        =   4
  67.       Top             =   4830
  68.       Width           =   7020
  69.       Begin VB.HScrollBar hbarBlue 
  70.          Enabled         =   0   'False
  71.          Height          =   255
  72.          LargeChange     =   16
  73.          Left            =   885
  74.          Max             =   255
  75.          TabIndex        =   7
  76.          Top             =   720
  77.          Width           =   6090
  78.       End
  79.       Begin VB.HScrollBar hbarGreen 
  80.          Enabled         =   0   'False
  81.          Height          =   255
  82.          LargeChange     =   16
  83.          Left            =   885
  84.          Max             =   255
  85.          TabIndex        =   6
  86.          Top             =   360
  87.          Width           =   6090
  88.       End
  89.       Begin VB.HScrollBar hbarRed 
  90.          Enabled         =   0   'False
  91.          Height          =   255
  92.          LargeChange     =   16
  93.          Left            =   885
  94.          Max             =   255
  95.          TabIndex        =   5
  96.          Top             =   0
  97.          Width           =   6090
  98.       End
  99.       Begin VB.Label lblBlue 
  100.          BorderStyle     =   1  'Fixed Single
  101.          Caption         =   "0"
  102.          Height          =   255
  103.          Left            =   480
  104.          TabIndex        =   13
  105.          Top             =   720
  106.          Width           =   375
  107.       End
  108.       Begin VB.Label lblGreen 
  109.          BorderStyle     =   1  'Fixed Single
  110.          Caption         =   "0"
  111.          Height          =   255
  112.          Left            =   480
  113.          TabIndex        =   12
  114.          Top             =   360
  115.          Width           =   375
  116.       End
  117.       Begin VB.Label lblRed 
  118.          BorderStyle     =   1  'Fixed Single
  119.          Caption         =   "0"
  120.          Height          =   255
  121.          Left            =   480
  122.          TabIndex        =   11
  123.          Top             =   0
  124.          Width           =   375
  125.       End
  126.       Begin VB.Label Label1 
  127.          Caption         =   "Red"
  128.          Height          =   255
  129.          Index           =   2
  130.          Left            =   0
  131.          TabIndex        =   10
  132.          Top             =   0
  133.          Width           =   495
  134.       End
  135.       Begin VB.Label Label1 
  136.          Caption         =   "Green"
  137.          Height          =   255
  138.          Index           =   1
  139.          Left            =   0
  140.          TabIndex        =   9
  141.          Top             =   360
  142.          Width           =   495
  143.       End
  144.       Begin VB.Label Label1 
  145.          Caption         =   "Blue"
  146.          Height          =   255
  147.          Index           =   0
  148.          Left            =   0
  149.          TabIndex        =   8
  150.          Top             =   720
  151.          Width           =   495
  152.       End
  153.    End
  154.    Begin VB.PictureBox picHidden 
  155.       AutoRedraw      =   -1  'True
  156.       Height          =   495
  157.       Left            =   3720
  158.       Picture         =   "PalEdit.frx":0CD2
  159.       ScaleHeight     =   29
  160.       ScaleMode       =   3  'Pixel
  161.       ScaleWidth      =   29
  162.       TabIndex        =   0
  163.       Top             =   4560
  164.       Visible         =   0   'False
  165.       Width           =   495
  166.    End
  167.    Begin VB.HScrollBar HBar 
  168.       Height          =   255
  169.       Left            =   0
  170.       TabIndex        =   3
  171.       Top             =   4530
  172.       Width           =   4245
  173.    End
  174.    Begin VB.VScrollBar VBar 
  175.       Height          =   4515
  176.       Left            =   4260
  177.       TabIndex        =   2
  178.       Top             =   0
  179.       Width           =   255
  180.    End
  181.    Begin VB.Menu mnuFile 
  182.       Caption         =   "&File"
  183.       Begin VB.Menu mnuFileOpen 
  184.          Caption         =   "&Open..."
  185.          Shortcut        =   ^O
  186.       End
  187.       Begin VB.Menu mnuFileSave 
  188.          Caption         =   "&Save"
  189.          Enabled         =   0   'False
  190.          Shortcut        =   ^S
  191.       End
  192.       Begin VB.Menu mnuFileSaveAs 
  193.          Caption         =   "Save &As..."
  194.          Shortcut        =   ^A
  195.       End
  196.       Begin VB.Menu mnuFileSep1 
  197.          Caption         =   "-"
  198.       End
  199.       Begin VB.Menu mnuFileRevert 
  200.          Caption         =   "&Revert"
  201.          Enabled         =   0   'False
  202.          Shortcut        =   ^R
  203.       End
  204.       Begin VB.Menu mnuFileSep2 
  205.          Caption         =   "-"
  206.       End
  207.       Begin VB.Menu mnuFileExit 
  208.          Caption         =   "E&xit"
  209.       End
  210.    End
  211.    Begin VB.Menu mnuScale 
  212.       Caption         =   "&Scale"
  213.       Begin VB.Menu mnuScaleZoomIn 
  214.          Caption         =   "Zoom &In"
  215.          Shortcut        =   ^I
  216.       End
  217.       Begin VB.Menu mnuScaleFull 
  218.          Caption         =   "&Full Scale"
  219.       End
  220.       Begin VB.Menu mnuScaleZoomOut 
  221.          Caption         =   "Zoom &Out"
  222.       End
  223.    End
  224.    Begin VB.Menu mnuColor 
  225.       Caption         =   "&Color"
  226.       Begin VB.Menu mnuNear 
  227.          Caption         =   "&Nearest"
  228.          Begin VB.Menu mnuNearRed 
  229.             Caption         =   "&Red"
  230.          End
  231.          Begin VB.Menu mnuNearGreen 
  232.             Caption         =   "&Green"
  233.          End
  234.          Begin VB.Menu mnuNearBlue 
  235.             Caption         =   "&Blue"
  236.          End
  237.          Begin VB.Menu mnuNearGray 
  238.             Caption         =   "Gray"
  239.          End
  240.       End
  241.       Begin VB.Menu mnuGrad 
  242.          Caption         =   "&Gradient"
  243.          Begin VB.Menu mnuGradRed 
  244.             Caption         =   "&Red"
  245.          End
  246.          Begin VB.Menu mnuGradGreen 
  247.             Caption         =   "&Green"
  248.          End
  249.          Begin VB.Menu mnuGradBlue 
  250.             Caption         =   "&Blue"
  251.          End
  252.          Begin VB.Menu mnuGradGray 
  253.             Caption         =   "Gray"
  254.          End
  255.          Begin VB.Menu mnuGradRainbow 
  256.             Caption         =   "Rainbow"
  257.          End
  258.       End
  259.    End
  260. Attribute VB_Name = "frmPalEdit"
  261. Attribute VB_GlobalNameSpace = False
  262. Attribute VB_Creatable = False
  263. Attribute VB_PredeclaredId = True
  264. Attribute VB_Exposed = False
  265. Option Explicit
  266. Private Const PALETTE_RELATIVE = &H2000000
  267. Private Type RECT
  268.     Left As Long
  269.     Top As Long
  270.     Right As Long
  271.     Bottom As Long
  272. End Type
  273. Private Type BITMAP
  274.     bmType As Long
  275.     bmWidth As Long
  276.     bmHeight As Long
  277.     bmWidthBytes As Long
  278.     bmPlanes As Integer
  279.     bmBitsPixel As Integer
  280.     bmBits As Long
  281. End Type
  282. Private Type PALETTEENTRY
  283.     peRed As Byte
  284.     peGreen As Byte
  285.     peBlue As Byte
  286.     peFlags As Byte
  287. End Type
  288. Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long
  289. Private Declare Function ResizePalette Lib "gdi32" (ByVal hPalette As Long, ByVal nNumEntries As Long) As Long
  290. Private Declare Function SetPaletteEntries Lib "gdi32" (ByVal hPalette As Long, ByVal wStartIndex As Long, ByVal wNumEntries As Long, lpPaletteEntries As PALETTEENTRY) As Long
  291. Private Declare Function GetPaletteEntries Lib "gdi32" (ByVal hPalette As Long, ByVal wStartIndex As Long, ByVal wNumEntries As Long, lpPaletteEntries As PALETTEENTRY) As Long
  292. Private Declare Function GetSystemPaletteEntries Lib "gdi32" (ByVal hdc As Long, ByVal wStartIndex As Long, ByVal wNumEntries As Long, lpPaletteEntries As PALETTEENTRY) As Long
  293. Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
  294. Private Declare Function GetObjectAPI Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
  295. Private Declare Function RealizePalette Lib "gdi32" (ByVal hdc As Long) As Long
  296. Private Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long
  297. Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
  298. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
  299. Private Const PC_EXPLICIT = &H2      ' Match to system palette index.
  300. Private Const PC_NOCOLLAPSE = &H4    ' Do not match color existing entries.
  301. ' GetDeviceCaps constants.
  302. Private Const RASTERCAPS = 38    ' Raster device capabilities.
  303. Private Const RC_PALETTE = &H100 ' Has palettes.
  304. Private Const NUMRESERVED = 106  ' # reserved entries in palette.
  305. Private Const SIZEPALETTE = 104  ' Size of system palette.
  306. Private Const NO_COLOR = -1
  307. Private LogicalPalette As Long
  308. Private SystemPalette As Long
  309. Private SysPalSize As Integer
  310. Private NumStaticColors As Integer
  311. Private StaticColor1 As Integer
  312. Private StaticColor2 As Integer
  313. Private SelectedI As Integer
  314. Private SelectedJ As Integer
  315. Private SelectedColor As Integer
  316. Private SelectedR As Integer
  317. Private SelectedG As Integer
  318. Private SelectedB As Integer
  319. Private Dx As Integer
  320. Private Dy As Integer
  321. Private SWid As Single
  322. Private SHgt As Single
  323. Private IWid As Single
  324. Private IHgt As Single
  325. Private ImageScale As Single
  326. Private SettingColor As Boolean
  327. Private DataChanged As Boolean
  328. Private FileLoaded As String
  329. ' If the data has been modified, allow the user
  330. ' to save the changes or cancel the operation.
  331. ' Return True if:
  332. '   - The image data has not been changed since
  333. '       it was loaded.
  334. '   - The user saves the changes.
  335. '   - The user says not to save.
  336. ' Return False otherwise.
  337. Function DataSafe() As Boolean
  338.     DataSafe = True
  339.     ' This is done in a while loop in case the
  340.     ' user starts a save and then cancels.
  341.     Do While DataChanged
  342.         Select Case MsgBox("The data has been modified. Do you want to save the changes?", vbQuestion + vbYesNoCancel, "Data Modified")
  343.             Case vbYes
  344.                 If FileLoaded <> "" Then
  345.                     mnuFileSave_Click
  346.                 Else
  347.                     mnuFileSaveAs_Click
  348.                 End If
  349.                 DataSafe = Not DataChanged
  350.             
  351.             Case vbNo
  352.                 DataSafe = True
  353.                 Exit Do
  354.             Case vbCancel
  355.                 DataSafe = False
  356.                 Exit Do
  357.         End Select
  358.     Loop
  359. End Function
  360. ' Copy the image from picHidden to picVisible at
  361. ' the correct scale.
  362. Private Sub DrawImage()
  363. Dim image_wid As Single
  364. Dim image_hgt As Single
  365. Dim hidden_wid As Single
  366. Dim hidden_hgt As Single
  367.     If Not Visible Then Exit Sub
  368.     ' Fill it with white. Cls would redisplay the
  369.     ' Picture which is bad if ImageScale < 1.
  370.     picVisible.Line (0, 0)-(IWid, IHgt), vbWhite, BF
  371.     ' Copy the picture at the correct scale.
  372.     image_wid = picVisible.ScaleWidth
  373.     image_hgt = picVisible.ScaleHeight
  374.     hidden_wid = image_wid / ImageScale
  375.     hidden_hgt = image_hgt / ImageScale
  376.     picVisible.PaintPicture _
  377.         picHidden.Picture, 0, 0, _
  378.         image_wid, image_hgt, _
  379.         HBar.Value, VBar.Value, _
  380.         hidden_wid, hidden_hgt
  381. End Sub
  382. ' Load the indicated file and prepare to work
  383. ' with its palette.
  384. Private Sub LoadImage(fname As String)
  385.     On Error GoTo LoadFileError
  386.     picHidden.Picture = LoadPicture(fname)
  387.     ImageScale = 1#
  388.     ResetScrollBars
  389.     On Error GoTo LoadPalError
  390.     LoadLogicalPalette
  391.     FileLoaded = fname
  392.     Caption = "PalEdit [" & fname & "]"
  393.     mnuFileSave.Enabled = True
  394.     mnuFileRevert.Enabled = True
  395.     DataChanged = False
  396.     Exit Sub
  397. LoadFileError:
  398.     Beep
  399.     MsgBox "Error loading file " & fname & "." & _
  400.         vbCrLf & Error$
  401.     Exit Sub
  402. LoadPalError:
  403.     Beep
  404.     MsgBox "Error loading logical palette." & _
  405.         vbCrLf & Error$
  406.     Exit Sub
  407. End Sub
  408. ' Set the Max and LargeChange properties for the
  409. ' image scroll bars.
  410. Private Sub ResetScrollBars()
  411. Dim change As Single
  412.     change = picVisible.ScaleWidth / ImageScale
  413.     If picHidden.ScaleWidth <= change Then
  414.         HBar.Value = 0
  415.         HBar.Enabled = False
  416.     Else
  417.         HBar.Max = picHidden.ScaleWidth - change
  418.         HBar.LargeChange = change
  419.         HBar.Enabled = True
  420.     End If
  421.     change = picVisible.ScaleHeight / ImageScale
  422.     If picHidden.ScaleHeight <= change Then
  423.         VBar.Value = 0
  424.         VBar.Enabled = False
  425.     Else
  426.         VBar.Max = picHidden.ScaleHeight - change
  427.         VBar.LargeChange = change
  428.         VBar.Enabled = True
  429.     End If
  430. End Sub
  431. ' Select the color with the indicated index.
  432. Private Sub SelectColorIndex(ByVal index As Integer)
  433. Dim i As Integer
  434. Dim j As Integer
  435.     i = index \ 16
  436.     j = index Mod 16
  437.     SelectColor i, j
  438. End Sub
  439. ' Load the picHidden palette so its entries
  440. ' match the system entries.
  441. Private Sub LoadLogicalPalette()
  442. Dim palentry(0 To 255) As PALETTEENTRY
  443. Dim blanked(0 To 255) As PALETTEENTRY
  444. Dim i As Integer
  445.     ' Make picVisible and picSwatch use the same
  446.     ' palette as picHidden.
  447.     picVisible.Picture = picHidden.Picture
  448.     picSwatch.Picture = picHidden.Picture
  449.     LogicalPalette = picHidden.Picture.hPal
  450.     ' Draw the image at the correct scale.
  451.     DrawImage
  452.     ' Make sure picVisible has the foreground palette.
  453.     RealizePalette picVisible.hdc
  454.     ' Give the system a chance to catch up.
  455.     DoEvents
  456.     ' Make the logical palette as big as possible.
  457.     If ResizePalette(LogicalPalette, SysPalSize) = 0 Then
  458.         MsgBox "Error resizing logical palette."
  459.         Exit Sub
  460.     End If
  461.     ' Get the system palette entries.
  462.     GetSystemPaletteEntries picHidden.hdc, 0, SysPalSize, palentry(0)
  463.     ' Blank the non-static colors.
  464.     For i = 0 To StaticColor1
  465.         blanked(i) = palentry(i)
  466.         blanked(i).peFlags = PC_NOCOLLAPSE
  467.     Next i
  468.     For i = StaticColor1 + 1 To StaticColor2 - 1
  469.         With blanked(i)
  470.             .peRed = i
  471.             .peGreen = 0
  472.             .peBlue = 0
  473.             .peFlags = PC_NOCOLLAPSE
  474.         End With
  475.     Next i
  476.     For i = StaticColor2 To 255
  477.         blanked(i) = palentry(i)
  478.         blanked(i).peFlags = PC_NOCOLLAPSE
  479.     Next i
  480.     SetPaletteEntries LogicalPalette, 0, SysPalSize, blanked(0)
  481.     ' Insert the non-static colors.
  482.     For i = StaticColor1 + 1 To StaticColor2 - 1
  483.         palentry(i).peFlags = PC_NOCOLLAPSE
  484.     Next i
  485.     SetPaletteEntries LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1)
  486.     ' Realize the new palette values.
  487.     RealizePalette picVisible.hdc
  488.     ' Select the color that was selected before.
  489.     SelectColor SelectedI, SelectedJ
  490. End Sub
  491. ' Load the picSystemColors palette with PC_EXPLICIT
  492. ' entries so they match the system palette.
  493. Private Sub LoadSystemPalette()
  494. Dim palentry(0 To 255) As PALETTEENTRY
  495. Dim i As Integer
  496.     ' Make the logical palette as big as possible.
  497.     SystemPalette = picSystemColors.Picture.hPal
  498.     If ResizePalette(SystemPalette, SysPalSize) = 0 Then
  499.         Beep
  500.         MsgBox "Error resizing system palette.", _
  501.             vbExclamation
  502.         Exit Sub
  503.     End If
  504.     ' Flag all palette entries as PC_EXPLICIT.
  505.     ' Set peRed to the system palette indexes.
  506.     For i = 0 To SysPalSize - 1
  507.         palentry(i).peRed = i
  508.         palentry(i).peFlags = PC_EXPLICIT
  509.     Next i
  510.     ' Update the palette (ignore return value).
  511.     i = SetPaletteEntries(SystemPalette, 0, SysPalSize, palentry(0))
  512. End Sub
  513. ' Fill the system picture with all the palette
  514. ' colors, hatching the static colors.
  515. Private Sub ShowpicSystemColors()
  516. Dim i As Integer
  517. Dim j As Integer
  518. Dim clr As Integer
  519. Dim oldfill As Integer
  520. Dim olddraw As Integer
  521.     picSystemColors.Cls
  522.     ' Display the colors using palette indexing.
  523.     Dx = picSystemColors.ScaleWidth / 16
  524.     Dy = picSystemColors.ScaleHeight / 16
  525.     clr = 0
  526.     For i = 0 To 15
  527.         For j = 0 To 15
  528.             picSystemColors.Line _
  529.                 (j * Dx, i * Dy)-Step(Dx, Dy), _
  530.                 clr + &H1000000, BF
  531.             clr = clr + 1
  532.         Next j
  533.     Next i
  534.     ' Hatch the static colors.
  535.     oldfill = picSystemColors.FillStyle
  536.     olddraw = picSystemColors.DrawMode
  537.     picSystemColors.FillStyle = vbDownwardDiagonal
  538.     picSystemColors.DrawMode = vbInvisible
  539.     picSystemColors.Line (0, 0)-Step((NumStaticColors \ 2) * Dx - 1, Dy - 1), , B
  540.     picSystemColors.Line (16 * Dx, 16 * Dy)-Step(-(NumStaticColors \ 2) * Dx, -Dy), , B
  541.     picSystemColors.FillStyle = oldfill
  542.     picSystemColors.DrawMode = olddraw
  543.     ' Highlight color (0, 0).
  544.     SelectedColor = NO_COLOR
  545.     SelectColor 0, 0
  546. End Sub
  547. ' Select the color at the indicated position.
  548. Private Sub SelectColor(ByVal i As Integer, ByVal j As Integer)
  549. Const GAP1 = 1
  550. Const GAP2 = 2
  551. Const DRAW_WID = 2
  552. Dim oldmode As Integer
  553. Dim oldwid As Integer
  554.     oldmode = picSystemColors.DrawMode
  555.     oldwid = picSystemColors.DrawWidth
  556.     picSystemColors.DrawMode = vbInvert
  557.     picSystemColors.DrawWidth = DRAW_WID
  558.     ' Unhighlight the previously selected color.
  559.     If SelectedColor <> NO_COLOR Then _
  560.         picSystemColors.Line (SelectedJ * Dx + GAP1, SelectedI * Dx + GAP1)-Step(Dx - GAP2, Dx - GAP2), , B
  561.     ' Record the new color.
  562.     SelectedI = i
  563.     SelectedJ = j
  564.     SelectedColor = i * 16 + j
  565.     ' Highlight the new color.
  566.     picSystemColors.Line (SelectedJ * Dx + GAP1, SelectedI * Dx + GAP1)-Step(Dx - GAP2, Dx - GAP2), , B
  567.     picSystemColors.DrawMode = oldmode
  568.     picSystemColors.DrawWidth = oldwid
  569.     ' Display the color's components.
  570.     ShowColorValue
  571. End Sub
  572. ' Display the selected color's components in the
  573. ' colors labels and scroll bars.
  574. Private Sub ShowColorValue()
  575. Dim palentry As PALETTEENTRY
  576. Dim status As Integer
  577.     If SelectedColor = NO_COLOR Then Exit Sub
  578.     status = GetSystemPaletteEntries(picSystemColors.hdc, SelectedColor, 1, palentry)
  579.     ' Update the labels.
  580.     lblRed.Caption = Format$(palentry.peRed)
  581.     lblGreen.Caption = Format$(palentry.peGreen)
  582.     lblBlue.Caption = Format$(palentry.peBlue)
  583.     ' Update the color swatch.
  584.     picSwatch.Line (0, 0)-(SWid, SHgt), RGB(palentry.peRed, palentry.peGreen, palentry.peBlue) + PALETTE_RELATIVE, BF
  585.     ' Update the scroll bars.
  586.     If SelectedColor > StaticColor1 And SelectedColor < StaticColor2 Then
  587.         SettingColor = True
  588.         hbarRed.Value = palentry.peRed
  589.         hbarGreen.Value = palentry.peGreen
  590.         hbarBlue.Value = palentry.peBlue
  591.         SettingColor = False
  592.         hbarRed.Enabled = True
  593.         hbarGreen.Enabled = True
  594.         hbarBlue.Enabled = True
  595.     Else
  596.         hbarRed.Enabled = False
  597.         hbarGreen.Enabled = False
  598.         hbarBlue.Enabled = False
  599.     End If
  600. End Sub
  601. ' Update the selected color's value.
  602. Private Sub UpdatePalette()
  603. Dim pe As PALETTEENTRY
  604. Dim i As Integer
  605.     pe.peRed = hbarRed.Value
  606.     pe.peGreen = hbarGreen.Value
  607.     pe.peBlue = hbarBlue.Value
  608.     pe.peFlags = PC_NOCOLLAPSE
  609.     ' Update the hidden picture's palette.
  610.     SetPaletteEntries LogicalPalette, SelectedColor, 1, pe
  611.     RealizePalette picHidden.hdc
  612.     picHidden.Picture = picHidden.Image
  613. 'picVisible.Picture = picHidden.Picture
  614. 'picSwatch.Picture = picHidden.Picture
  615. 'LogicalPalette = picHidden.Picture.hPal
  616. 'DrawImage
  617. 'Palette = picVisible.Picture
  618. 'PaletteMode = vbPaletteModeCustom
  619.     SetPaletteEntries picVisible.Picture.hPal, SelectedColor, 1, pe
  620.     RealizePalette picVisible.hdc
  621.     picVisible.Picture = picVisible.Image
  622.     SetPaletteEntries picSwatch.Picture.hPal, SelectedColor, 1, pe
  623.     RealizePalette picSwatch.hdc
  624.     picSwatch.Picture = picSwatch.Image
  625.     picSwatch.Line (0, 0)-(SWid, SHgt), RGB(pe.peRed, pe.peGreen, pe.peBlue) + PALETTE_RELATIVE, BF
  626.     DataChanged = True
  627. End Sub
  628. ' Update the selected color's value.
  629. Private Sub hbarBlue_Change()
  630.     If SettingColor Then Exit Sub
  631.     lblBlue.Caption = Format$(hbarBlue.Value)
  632.     UpdatePalette
  633. End Sub
  634. ' Update the selected color's value.
  635. Private Sub hbarBlue_Scroll()
  636.     If SettingColor Then Exit Sub
  637.     lblBlue.Caption = Format$(hbarBlue.Value)
  638.     UpdatePalette
  639. End Sub
  640. ' Make the scroll bars as big as possible within picColors.
  641. Private Sub picColors_Resize()
  642. Dim wid As Single
  643.     wid = picColors.ScaleWidth - lblRed.Left - lblRed.Width - 2
  644.     If wid < 10 Then wid = 10
  645.     hbarRed.Width = wid
  646.     hbarGreen.Width = wid
  647.     hbarBlue.Width = wid
  648. End Sub
  649. ' 1. Make sure we can handle palettes.
  650. ' 2. Find out how big the system palette is and how
  651. ' many static colors there are.
  652. ' 3. Load and display the system palette.
  653. Private Sub Form_Load()
  654.     ' Make sure the screen supports palettes.
  655.     If Not GetDeviceCaps(hdc, RASTERCAPS) And RC_PALETTE Then
  656.         MsgBox "This system is not using palettes."
  657.         End
  658.     End If
  659.     ' Get system palette size and # static colors.
  660.     SysPalSize = GetDeviceCaps(hdc, SIZEPALETTE)
  661.     NumStaticColors = GetDeviceCaps(hdc, NUMRESERVED)
  662.     StaticColor1 = NumStaticColors \ 2 - 1
  663.     StaticColor2 = SysPalSize - NumStaticColors \ 2
  664.     picHidden.AutoSize = True
  665.     ImageScale = 1#
  666.     ' Load the system palette.
  667.     LoadSystemPalette
  668.     ' Display the system palette.
  669.     ShowpicSystemColors
  670.     ' Load the logical palette.
  671.     LoadLogicalPalette
  672.     ' Start in the current directory.
  673.     dlgOpenFile.InitDir = App.Path
  674. End Sub
  675. ' Refuse to unload if there are unsaved changes.
  676. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  677.     Cancel = Not DataSafe()
  678. End Sub
  679. ' Make the picture as large as possible.
  680. Private Sub Form_Resize()
  681. Dim L As Single
  682. Dim T As Single
  683. Dim wid As Single
  684. Dim hgt As Single
  685.     If WindowState = vbMinimized Then Exit Sub
  686.     ' Keep system colors in the upper right corner.
  687.     picSystemColors.Move ScaleWidth - picSystemColors.Width
  688.     ' Keep color box stretched across the bottom.
  689.     picColors.Move 0, ScaleHeight - picColors.Height, ScaleWidth
  690.     ' Put color swatch under system colors.
  691.     hgt = picColors.Top - picSystemColors.Height - 6
  692.     If hgt < 10 Then hgt = 10
  693.     picSwatch.Move picSystemColors.Left, picSystemColors.Height + 3, picSwatch.Width, hgt
  694.     SWid = picSwatch.ScaleWidth - 1
  695.     SHgt = picSwatch.ScaleHeight - 1
  696.     ' Place the vertical scroll bar.
  697.     L = picSystemColors.Left - VBar.Width - 3
  698.     hgt = picColors.Top - HBar.Height - 4
  699.     If hgt < 10 Then hgt = 10
  700.     VBar.Move L, 0, VBar.Width, hgt
  701.     ' Place the horizontal scroll bar.
  702.     T = picColors.Top - HBar.Height - 3
  703.     wid = picSystemColors.Left - VBar.Width - 4
  704.     If wid < 10 Then wid = 10
  705.     HBar.Move 0, T, wid
  706.         
  707.     ' Place picVisible inside the scroll bars.
  708.     picVisible.Move 0, 0, wid, hgt
  709.     IWid = picVisible.ScaleWidth - 1
  710.     IHgt = picVisible.ScaleHeight - 1
  711.     ' Set the scroll bar limits.
  712.     ResetScrollBars
  713.     ' Redraw the image in case we've grown.
  714.     DrawImage
  715.     ' Refill picSwatch (it may have grown).
  716.     ShowColorValue
  717. End Sub
  718. ' Update the selected color's value.
  719. Private Sub hbarGreen_Change()
  720.     If SettingColor Then Exit Sub
  721.     lblGreen.Caption = Format$(hbarGreen.Value)
  722.     UpdatePalette
  723. End Sub
  724. ' Update the selected color's value.
  725. Private Sub hbarGreen_Scroll()
  726.     If SettingColor Then Exit Sub
  727.     lblGreen.Caption = Format$(hbarGreen.Value)
  728.     UpdatePalette
  729. End Sub
  730. ' Redraw the image scrolled appropriately.
  731. Private Sub HBar_Change()
  732.     DrawImage
  733. End Sub
  734. ' Redraw the image scrolled appropriately.
  735. Private Sub HBar_Scroll()
  736.     DrawImage
  737. End Sub
  738. ' Select the color the user clicked on.
  739. Private Sub picVisible_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  740. Dim bm As BITMAP
  741. Dim hbm As Long
  742. Dim status As Long
  743. Dim bytes() As Byte
  744. Dim wid As Long
  745. Dim hgt As Long
  746.     ' Get a handle to the bitmap.
  747.     hbm = picVisible.Image
  748.     ' See how big it is.
  749.     status = GetObjectAPI(hbm, Len(bm), bm)
  750.     wid = bm.bmWidthBytes
  751.     hgt = bm.bmHeight
  752.     ' If the mouse is out of bounds, bail out.
  753.     If X >= wid Or Y >= hgt Then
  754.         Beep
  755.         Exit Sub
  756.     End If
  757.     ' Get the bits.
  758.     ReDim bytes(0 To wid - 1, 0 To hgt - 1)
  759.     status = GetBitmapBits(hbm, wid * hgt, bytes(0, 0))
  760.     ' Select the color of this pixel.
  761.     SelectColorIndex bytes(CInt(X), CInt(Y))
  762. End Sub
  763. ' Load a new image file.
  764. Private Sub mnuFileOpen_Click()
  765. Dim fname As String
  766.     ' Make sure any changes have been saved.
  767.     If Not DataSafe() Then Exit Sub
  768.     ' Allow the user to pick a file.
  769.     On Error Resume Next
  770.     dlgOpenFile.FileName = "*.BMP;*.WMF;*.DIB;*.JPG;*.GIF"
  771.     dlgOpenFile.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  772.     dlgOpenFile.ShowOpen
  773.     If Err.Number = cdlCancel Then
  774.         Exit Sub
  775.     ElseIf Err.Number <> 0 Then
  776.         Beep
  777.         MsgBox "Error selecting file.", , vbExclamation
  778.         Exit Sub
  779.     End If
  780.     On Error GoTo 0
  781.     fname = Trim$(dlgOpenFile.FileName)
  782.     dlgOpenFile.InitDir = Left$(fname, Len(fname) _
  783.         - Len(dlgOpenFile.FileTitle) - 1)
  784.     ' Load the picture.
  785.     Screen.MousePointer = vbHourglass
  786.     DoEvents
  787.     LoadImage fname
  788.     Screen.MousePointer = vbDefault
  789. End Sub
  790. ' Reload the file.
  791. Private Sub mnuFileRevert_Click()
  792.     ' If the data has changed, get confirmation.
  793.     If DataChanged Then
  794.         If MsgBox("The data has been modified. Are you sure you want to remove the changes?", _
  795.             vbQuestion + vbYesNo) = vbNo Then _
  796.                 Exit Sub
  797.     End If
  798.     ' Reload the picture.
  799.     Screen.MousePointer = vbHourglass
  800.     DoEvents
  801.     LoadImage FileLoaded
  802.     Screen.MousePointer = vbDefault
  803. End Sub
  804. ' Save the image in the file from which it was
  805. ' loaded.
  806. Private Sub mnuFileSave_Click()
  807.     Screen.MousePointer = vbHourglass
  808.     DoEvents
  809.     SaveImage FileLoaded
  810.     Screen.MousePointer = vbDefault
  811. End Sub
  812. ' Save the image in a new file.
  813. Private Sub mnuFileSaveAs_Click()
  814. Dim fname As String
  815.     ' Allow the user to pick a file.
  816.     On Error Resume Next
  817.     dlgOpenFile.FileName = "*.BMP;*.ICO;*.RLE;*.WMF;*.DIB"
  818.     dlgOpenFile.Flags = cdlOFNOverwritePrompt + _
  819.         cdlOFNHideReadOnly + cdlOFNPathMustExist
  820.     dlgOpenFile.ShowSave
  821.     If Err.Number = cdlCancel Then
  822.         Exit Sub
  823.     ElseIf Err.Number <> 0 Then
  824.         Beep
  825.         MsgBox "Error selecting file.", , vbExclamation
  826.         Exit Sub
  827.     End If
  828.     On Error GoTo 0
  829.     fname = Trim$(dlgOpenFile.FileName)
  830.     dlgOpenFile.InitDir = Left$(fname, Len(fname) _
  831.         - Len(dlgOpenFile.FileTitle) - 1)
  832.     ' Save the picture.
  833.     Screen.MousePointer = vbHourglass
  834.     DoEvents
  835.     SaveImage fname
  836.     Screen.MousePointer = vbDefault
  837. End Sub
  838. ' Save the picture in the indicated file.
  839. Private Sub SaveImage(fname As String)
  840.     On Error GoTo SaveError
  841.     picVisible.Picture = picVisible.Image
  842.     SavePicture picVisible.Picture, fname
  843.     Caption = "PalEdit [" & fname & "]"
  844.     FileLoaded = fname
  845.     DataChanged = False
  846.     Exit Sub
  847. SaveError:
  848.     Beep
  849.     MsgBox "Error saving picture in file " & _
  850.         fname & "." & vbCrLf & vbCrLf & _
  851.         Error$, , vbExclamation
  852.     Exit Sub
  853. End Sub
  854. ' Replace colors with a green gradient.
  855. Private Sub mnuGradGreen_Click()
  856. Dim palentry(0 To 255) As PALETTEENTRY
  857. Dim i As Integer
  858. Dim g As Single
  859. Dim Dg As Single
  860.     Dg = 255 / (StaticColor2 - StaticColor1)
  861.     g = Dg
  862.     For i = StaticColor1 + 1 To StaticColor2 - 1
  863.         With palentry(i)
  864.             .peRed = 0
  865.             .peGreen = g
  866.             .peBlue = 0
  867.             .peFlags = PC_NOCOLLAPSE
  868.         End With
  869.         g = g + Dg
  870.     Next i
  871.     If SetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1)) = 0 Then
  872.         Beep
  873.         MsgBox "Error resetting colors.", , vbExclamation
  874.         Exit Sub
  875.     End If
  876.     i = RealizePalette(picVisible.hdc)
  877.     DataChanged = True
  878. End Sub
  879. ' Replace colors with red, green, and blue
  880. ' gradients.
  881. Private Sub mnuGradRainbow_Click()
  882. Dim palentry(0 To 255) As PALETTEENTRY
  883. Dim i As Integer
  884. Dim num_each As Integer
  885. Dim clr As Integer
  886. Dim c As Single
  887. Dim Dc As Single
  888.     num_each = (StaticColor2 - StaticColor1) / 3
  889.     Dc = 255 / num_each
  890.     clr = StaticColor1 + 1
  891.     ' Red shades.
  892.     c = Dc
  893.     For i = 1 To num_each
  894.         With palentry(clr)
  895.             .peRed = c
  896.             .peGreen = 0
  897.             .peBlue = 0
  898.             .peFlags = PC_NOCOLLAPSE
  899.         End With
  900.         c = c + Dc
  901.         clr = clr + 1
  902.     Next i
  903.     ' Green shades.
  904.     c = Dc
  905.     For i = 1 To num_each
  906.         With palentry(clr)
  907.             .peRed = 0
  908.             .peGreen = c
  909.             .peBlue = 0
  910.             .peFlags = PC_NOCOLLAPSE
  911.         End With
  912.         c = c + Dc
  913.         clr = clr + 1
  914.     Next i
  915.     ' Blue shades.
  916.     c = Dc
  917.     For i = clr To StaticColor2 - 1
  918.         With palentry(clr)
  919.             .peRed = 0
  920.             .peGreen = 0
  921.             .peBlue = c
  922.             .peFlags = PC_NOCOLLAPSE
  923.         End With
  924.         c = c + Dc
  925.         clr = clr + 1
  926.     Next i
  927.     If SetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1)) = 0 Then
  928.         Beep
  929.         MsgBox "Error resetting colors.", , vbExclamation
  930.         Exit Sub
  931.     End If
  932.     i = RealizePalette(picVisible.hdc)
  933.     DataChanged = True
  934. End Sub
  935. ' Replace colors with a red gradient.
  936. Private Sub mnuGradRed_Click()
  937. Dim palentry(0 To 255) As PALETTEENTRY
  938. Dim i As Integer
  939. Dim r As Single
  940. Dim Dr As Single
  941.     Dr = 255 / (StaticColor2 - StaticColor1)
  942.     r = Dr
  943.     For i = StaticColor1 + 1 To StaticColor2 - 1
  944.         With palentry(i)
  945.             .peRed = r
  946.             .peGreen = 0
  947.             .peBlue = 0
  948.             .peFlags = PC_NOCOLLAPSE
  949.         End With
  950.         r = r + Dr
  951.     Next i
  952.     If SetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1)) = 0 Then
  953.         Beep
  954.         MsgBox "Error resetting colors.", , vbExclamation
  955.         Exit Sub
  956.     End If
  957.     i = RealizePalette(picVisible.hdc)
  958.     DataChanged = True
  959. End Sub
  960. ' Replace colors with appropriate greens.
  961. Private Sub mnuNearGreen_Click()
  962. Dim palentry(0 To 255) As PALETTEENTRY
  963. Dim i As Integer
  964. Dim clr As Integer
  965.     ' Get the current color values.
  966.     i = GetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1))
  967.     ' Fill in the nearest shades.
  968.     For i = StaticColor1 + 1 To StaticColor2 - 1
  969.         With palentry(i)
  970.             clr = (CInt(.peRed) + .peGreen + .peBlue) / 3
  971.             .peRed = 0
  972.             .peGreen = clr
  973.             .peBlue = 0
  974.             .peFlags = PC_NOCOLLAPSE
  975.         End With
  976.     Next i
  977.     If SetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1)) = 0 Then
  978.         Beep
  979.         MsgBox "Error resetting colors.", , vbExclamation
  980.         Exit Sub
  981.     End If
  982.     i = RealizePalette(picVisible.hdc)
  983.     DataChanged = True
  984. End Sub
  985. ' Replace colors with appropriate reds.
  986. Private Sub mnuNearRed_Click()
  987. Dim palentry(0 To 255) As PALETTEENTRY
  988. Dim i As Integer
  989. Dim clr As Integer
  990.     ' Get the current color values.
  991.     i = GetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1))
  992.     ' Fill in the nearest shades.
  993.     For i = StaticColor1 + 1 To StaticColor2 - 1
  994.         With palentry(i)
  995.             clr = (CInt(.peRed) + .peGreen + .peBlue) / 3
  996.             .peRed = clr
  997.             .peGreen = 0
  998.             .peBlue = 0
  999.             .peFlags = PC_NOCOLLAPSE
  1000.         End With
  1001.     Next i
  1002.     If SetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1)) = 0 Then
  1003.         Beep
  1004.         MsgBox "Error resetting colors.", , vbExclamation
  1005.         Exit Sub
  1006.     End If
  1007.     i = RealizePalette(picVisible.hdc)
  1008.     DataChanged = True
  1009. End Sub
  1010. ' Replace colors with appropriate grays.
  1011. Private Sub mnuNearGray_Click()
  1012. Dim palentry(0 To 255) As PALETTEENTRY
  1013. Dim i As Integer
  1014. Dim clr As Integer
  1015.     ' Get the current color values.
  1016.     i = GetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1))
  1017.     ' Fill in the nearest shades.
  1018.     For i = StaticColor1 + 1 To StaticColor2 - 1
  1019.         With palentry(i)
  1020.             clr = (CInt(.peRed) + .peGreen + .peBlue) / 3
  1021.             .peRed = clr
  1022.             .peGreen = clr
  1023.             .peBlue = clr
  1024.             .peFlags = PC_NOCOLLAPSE
  1025.         End With
  1026.     Next i
  1027.     If SetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1)) = 0 Then
  1028.         Beep
  1029.         MsgBox "Error resetting colors.", , vbExclamation
  1030.         Exit Sub
  1031.     End If
  1032.     i = RealizePalette(picVisible.hdc)
  1033.     DataChanged = True
  1034. End Sub
  1035. ' Replace colors with appropriate blues.
  1036. Private Sub mnuNearBlue_Click()
  1037. Dim palentry(0 To 255) As PALETTEENTRY
  1038. Dim i As Integer
  1039. Dim clr As Integer
  1040.     ' Get the current color values.
  1041.     i = GetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1))
  1042.     ' Fill in the nearest shades.
  1043.     For i = StaticColor1 + 1 To StaticColor2 - 1
  1044.         With palentry(i)
  1045.             clr = (CInt(.peRed) + .peGreen + .peBlue) / 3
  1046.             .peRed = 0
  1047.             .peGreen = 0
  1048.             .peBlue = clr
  1049.             .peFlags = PC_NOCOLLAPSE
  1050.         End With
  1051.     Next i
  1052.     If SetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1)) = 0 Then
  1053.         Beep
  1054.         MsgBox "Error resetting colors.", , vbExclamation
  1055.         Exit Sub
  1056.     End If
  1057.     i = RealizePalette(picVisible.hdc)
  1058.     DataChanged = True
  1059. End Sub
  1060. ' Replace colors with a gray gradient.
  1061. Private Sub mnuGradGray_Click()
  1062. Dim palentry(0 To 255) As PALETTEENTRY
  1063. Dim i As Integer
  1064. Dim g As Single
  1065. Dim Dg As Single
  1066.     Dg = 255 / (StaticColor2 - StaticColor1)
  1067.     g = Dg
  1068.     For i = StaticColor1 + 1 To StaticColor2 - 1
  1069.         With palentry(i)
  1070.             .peRed = g
  1071.             .peGreen = g
  1072.             .peBlue = g
  1073.             .peFlags = PC_NOCOLLAPSE
  1074.         End With
  1075.         g = g + Dg
  1076.     Next i
  1077.     If SetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1)) = 0 Then
  1078.         Beep
  1079.         MsgBox "Error resetting colors.", , vbExclamation
  1080.         Exit Sub
  1081.     End If
  1082.     i = RealizePalette(picVisible.hdc)
  1083.     DataChanged = True
  1084. End Sub
  1085. ' Replace colors with a blue gradient.
  1086. Private Sub mnuGradBlue_Click()
  1087. Dim palentry(0 To 255) As PALETTEENTRY
  1088. Dim i As Integer
  1089. Dim b As Single
  1090. Dim Db As Single
  1091.     Db = 255 / (StaticColor2 - StaticColor1)
  1092.     b = Db
  1093.     For i = StaticColor1 + 1 To StaticColor2 - 1
  1094.         With palentry(i)
  1095.             .peRed = 0
  1096.             .peGreen = 0
  1097.             .peBlue = b
  1098.             .peFlags = PC_NOCOLLAPSE
  1099.         End With
  1100.         b = b + Db
  1101.     Next i
  1102.     If SetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1)) = 0 Then
  1103.         Beep
  1104.         MsgBox "Error resetting colors.", , vbExclamation
  1105.         Exit Sub
  1106.     End If
  1107.     i = RealizePalette(picVisible.hdc)
  1108.     DataChanged = True
  1109. End Sub
  1110. ' Set ImageScale = 1 and redraw the image.
  1111. Private Sub mnuScaleFull_Click()
  1112.     ImageScale = 1#
  1113.     ResetScrollBars
  1114.     DrawImage
  1115. End Sub
  1116. ' Increase ImageScale and redraw the image.
  1117. Private Sub mnuScaleZoomIn_Click()
  1118.     ImageScale = ImageScale * 2#
  1119.     ResetScrollBars
  1120.     DrawImage
  1121. End Sub
  1122. ' Decrease ImageScale and redraw the image.
  1123. Private Sub mnuScaleZoomOut_Click()
  1124.     ImageScale = ImageScale / 2#
  1125.     ResetScrollBars
  1126.     DrawImage
  1127. End Sub
  1128. ' Update the selected color's value.
  1129. Private Sub hbarRed_Change()
  1130.     If SettingColor Then Exit Sub
  1131.     lblRed.Caption = Format$(hbarRed.Value)
  1132.     UpdatePalette
  1133. End Sub
  1134. ' Update the selected color's value.
  1135. Private Sub hbarRed_Scroll()
  1136.     If SettingColor Then Exit Sub
  1137.     lblRed.Caption = Format$(hbarRed.Value)
  1138.     UpdatePalette
  1139. End Sub
  1140. ' Select the color the user clicked on.
  1141. Private Sub picSystemColors_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  1142. Dim i As Integer
  1143. Dim j As Integer
  1144.     i = Y \ Dx
  1145.     j = X \ Dy
  1146.     SelectColor i, j
  1147. End Sub
  1148. ' End the application. (See also the QueryUnload event.)
  1149. Private Sub mnuFileExit_Click()
  1150.     Unload Me
  1151. End Sub
  1152. ' Allow the user to select a new color with the arrow keys.
  1153. Private Sub picSystemColors_KeyDown(KeyCode As Integer, Shift As Integer)
  1154. Dim i As Integer
  1155. Dim j As Integer
  1156.     i = SelectedI
  1157.     j = SelectedJ
  1158.     Select Case KeyCode
  1159.         Case vbKeyDown
  1160.             i = i + 1
  1161.             If i * 16 + j >= SysPalSize Then i = 0
  1162.         
  1163.         Case vbKeyUp
  1164.             i = i - 1
  1165.             If i < 0 Then
  1166.                 i = (SysPalSize - 1) \ 16
  1167.                 If i * 16 + j >= SysPalSize Then _
  1168.                     i = i - 1
  1169.             End If
  1170.         
  1171.         Case vbKeyLeft
  1172.             j = j - 1
  1173.             If j < 0 Then
  1174.                 j = 15
  1175.                 If i * 16 + j >= SysPalSize Then _
  1176.                     j = SysPalSize - 1 - i * 16
  1177.             End If
  1178.         
  1179.         Case vbKeyRight
  1180.             j = j + 1
  1181.             If j > 15 Or _
  1182.                 i * 16 + j >= SysPalSize Then _
  1183.                     j = 0
  1184.         
  1185.     End Select
  1186.     SelectColor i, j
  1187. End Sub
  1188. ' Redraw the image scrolled appropriately.
  1189. Private Sub VBar_Change()
  1190.     DrawImage
  1191. End Sub
  1192. ' Redraw the image scrolled appropriately.
  1193. Private Sub VBar_Scroll()
  1194.     DrawImage
  1195. End Sub
  1196.